//获取到库的引用
const db = wx.cloud.database()

Page({
  data: {
    // 初始化
    imgList: [],
    filePath: '',
    realname: ''
  },
  // 上传图片
  mySelect(e) {
    // 获取到图片临时地址，是字符串数组 
    let arr = e.detail.tempFilePaths;
    console.log(arr);
    //上传图片
    let ext = arr.join().split(".").pop()
    let nowtime = new Date().getTime()
    console.log(nowtime, ext);
    wx.cloud.uploadFile({
      cloudPath: nowtime + "." + ext,
      filePath: arr.join(),
      success: res => {
        console.log(res)
      }

    })
    //把字符串数组，转化成对象数组
    let result = arr.map(item => {
      return {
        url: item
      }
    })
    // 渲染页面
    this.setData({
      imgList: result,
      filePath: result
    })
  },
  // 提交按钮
  handleUp() {
    let name = this.data.realname
    console.log(this.data.realname);
    let picList = this.data.filePath
    console.log(this.data.filePath);
    db.collection("demo").add({
      data: {
        realname: name,
        imgList: picList

      },
      // 点击提交后清空页面
      success: res => {
        this.setData({
          imgList: [],
          filePath: '',
          realname: ''
        })
        
      },
     
    })
  },
  // 跳转页面
  handleTo() {
    wx.navigateTo({
      url: '/pages/demo/demo',
    })
  }
})